﻿using System;
using System.Collections.Generic;

/// <summary>
/// Summary description for ArticlePreviews
/// </summary>
/// 

public abstract class BaseArticlePreview
{
	public long ID { get; private set; }
	public string Title { get; private set; }
	public string Preface { get; private set; }
	public DateTime PublishStopTime { get; private set; }
	public DateTime PublishStartTime { get; private set; }
	public string Author { get; private set; }
	public DateTime CreateTime { get; private set; }
	public long UserID { get; private set; }

	public BaseArticlePreview(long id, string title, string preface,
		DateTime publishStopTime, DateTime publishStartTime, string author,
		DateTime createTime, long userID)
	{
		ID = id;
		Title = title;
		Preface = preface;
		PublishStopTime = publishStopTime;
		PublishStartTime = publishStartTime;
		Author = author;
		CreateTime = createTime;
		UserID = userID;
	}
}


public class CategoryArticlePreview : BaseArticlePreview
{
	public bool Featured { get; private set; }

	public CategoryArticlePreview(long id, string title, string preface,
		DateTime publishStopTime, DateTime publishStartTime, string author,
		bool featured, DateTime createTime, long userID)
		:base(id, title, preface, publishStopTime, publishStartTime, author, createTime, userID)
	{
		Featured = featured;
	}
}

public class MainPageArticlePreview : BaseArticlePreview
{
	public long[] CategoryIDs { get; private set; }

	public MainPageArticlePreview(long id, string title, string preface, DateTime publishStartTime,
		DateTime publishStopTime, string author, DateTime createTime, long userID, long[] categoryIDs)
		: base(id, title, preface, publishStopTime, publishStartTime, author, createTime, userID)
	{
		CategoryIDs = categoryIDs;
	}
}

public class MyPageArticlePreview : MainPageArticlePreview
{
	public int PagesCount { get; private set; }
	public long[] NonCompletedPages { get; private set; }

	public MyPageArticlePreview(long id, string title, string preface, long[] categoryIDs,
		DateTime createTime, DateTime publishStartTime, DateTime publishStopTime,
		int pagesCount, long[] nonCompletedPages, long userID, string author)
		:base(id, title, preface, publishStartTime, publishStopTime, author, createTime, userID, categoryIDs)
	{
		PagesCount = pagesCount;
		NonCompletedPages = nonCompletedPages;
	}
}

public class Article : MainPageArticlePreview
{
	private Page[] pages;

	public Article(long id, string author, string title, string preface,
		long[] categoryIDs, Page[] pages, DateTime publishStartTime, 
		DateTime publishStopTime, DateTime createTime, long userID)
		:base(id, title, preface, publishStartTime, publishStopTime, author, createTime, userID, categoryIDs)
	{
		this.pages = pages;
	}

	public bool PublishedNow
	{
		get
		{
			return PublishStartTime <= DateTime.UtcNow
				&& PublishStopTime > DateTime.UtcNow;
		}
	}

	public Page[] GetPages(bool admin)
	{
		if(admin)
		{
			return pages;
		}
		else
		{
			List<Page> result = new List<Page>();
			foreach(var page in pages)
			{
				if(page.Status == CompleteStatus.Completed)
					result.Add(page);
			}

			return result.ToArray();
		}
	}

	public Page GetPage(long id, bool admin)
	{
		foreach(var page in pages)
		{
			if(page.ID == id)
			{
				if(page.Status != CompleteStatus.Completed
					&& !admin)
				{
					return null;
				}
				else
				{
					return page;
				}
			}
		}

		return null;
	}

	public long GetFirstPageID(bool admin)
	{
		long pageID = 0;
		foreach(var page in pages)
		{
			if(page.Status != CompleteStatus.Completed)
			{
				if(admin)
				{
					pageID = page.ID;
					break;
				}
			}
			else
			{
				pageID = page.ID;
				break;
			}
		}

		return pageID;
	}

	public int GetPagesCount(bool admin)
	{
		int count = 0;
		foreach(var page in pages)
		{
			if(page.Status != CompleteStatus.Completed)
			{
				if(admin)
					count++;
			}
			else
			{
				count++;
			}
		}

		return count;
	}

	public int GetPageIndex(bool admin, long pageID)
	{
		int count = 0;
		foreach(var page in pages)
		{
			if(page.ID == pageID)
				break;
			else
			{
				if(page.Status != CompleteStatus.Completed)
				{
					if(admin)
						count++;
				}
				else
				{
					count++;
				}
			}
		}

		return count;
	}

	public Section GetSection(long pageID, long sectionID)
	{
		foreach(var page in pages)
		{
			if(page.ID == pageID)
			{
				foreach(var section in page.Sections)
				{
					if(section.ID == sectionID)
						return section;
				}
			}
		}

		return null;
	}
}